/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package lab3serveurhttp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
/**
 *
 * @author Mathieu
 */
public class HTTPResponseHeader
{
    private HTTPState state = HTTPState.OK;
    private String connection = "Close";
    private Calendar date = Calendar.getInstance();
    private String server = "g33kttp/0.1.1 (" + System.getProperty("os.name") + ")";
    private Calendar lastModified = Calendar.getInstance ( );
    private long contentLength = 0;
    private String contentType = "text/html";
    
    public void setState ( HTTPState state )
    {
        this.state = state;
    }
    
    public void setConnection ( String connection )
    {
        this.connection = connection;
    }
    
    public void setDate ( Calendar date )
    {
        this.date = date;
    }
    
    public void setServer ( String server )
    {
        this.server = server;
    }
    
    public void setLastModified ( Calendar lastModified )
    {
        this.lastModified = lastModified;
    }
    
    public void setContentLength ( long length )
    {
        this.contentLength = length;
    }
    
    public void setContentType ( String contentType )
    {
        this.contentType = contentType;
    }
    
    public HTTPState getState ( )
    {
        return this.state;
    }
    
    @Override
    public String toString ( )
    {
        StringBuilder header = new StringBuilder ( );
        
        header.append ( "HTTP/1.1 ");
        header.append ( state.getValue() + " " );
        header.append ( state.toString () );
        header.append ( "\r\n" );
        
        header.append ( "Connection: " );
        header.append ( connection );
        header.append ( "\r\n" );
        
        header.append ( "Date: " );
        header.append (new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).format(date.getTime()));
        header.append ( "\r\n" );
        
        header.append ( "Server: " );
        header.append ( server );
        header.append ( "\r\n" );
        
        header.append ( "Last-Modified: " );
        header.append (new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US).format(lastModified.getTime()));
        header.append ( "\r\n" );    
        
        header.append ( "Content-Length: " );
        header.append ( contentLength );
        header.append ( "\r\n" );
        
        
        if ( this.contentType != null )
        {
            header.append ( "Content-Type: " );
            header.append ( contentType );
            header.append ( "\r\n" );
        }

        header.append ( "\r\n" );
        
        return header.toString ( );
    }
    
   
}
